package cn.demomaster.quickui_sample.ui.fragment.helper;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import cn.demomaster.quickpicturer.helper.PhotoHelper;
import cn.demomaster.quickrouter.fragment.QuickFragment;
import cn.demomaster.quickui.annotation.ActivityPager;
import cn.demomaster.quickui.annotation.ResType;
import com.demomaster.quickcompat.widget.toast.QdToast;
import cn.demomaster.quickui_sample.R;
import cn.demomaster.quickui_sample.ui.activity.MyCaptureActivity;
import cn.demomaster.quickzxing.CodeCreator;

/**
 * Squirrel桓
 * 2018/8/25
 */
@ActivityPager(name = "二维码", preViewClass = TextView.class, resType = ResType.Custome)
public class BarCodeFragment extends QuickFragment {

    Button btn_scan;
    EditText et_content;
    Button btn_generate;
    ImageView iv_code;

    @NonNull
    @Override
    public View onGenerateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View mView = inflater.inflate(R.layout.fragment_layout_barcode, null);
        return mView;
    }

    public void initView(View rootView) {
        btn_scan = findViewById(R.id.btn_scan);
        et_content = findViewById(R.id.et_content);
        btn_generate = findViewById(R.id.btn_generate);
        iv_code = findViewById(R.id.iv_code);
        btn_generate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = et_content.getText().toString();
                if (!TextUtils.isEmpty(content)) {
                    Bitmap bitmap = CodeCreator.createQRCode(content, 500, 500, null);
                    iv_code.setImageBitmap(bitmap);
                }
            }
        });
        btn_scan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
// TODO               PhotoHelper.Builder builder = new PhotoHelper.Builder(mContext)
//                        .setRequestType(RequestPhotoActionType.SCAN_QRCODE)
//                        .setScanActivityClass(MyCaptureActivity.class)
//                        .setOnTakePhotoResult(new PhotoHelper.OnTakePhotoResult() {
//                            @Override
//                            public void onSuccess(Intent data, String path) {
//                                if (!TextUtils.isEmpty(path)) {
//                                    QdToast.show(mContext,path);
//                                }
//                            }
//
//                            @Override
//                            public void onFailure(String error) {
//                                QdToast.show(mContext,error);
//                            }
//                        });
//                getPhotoHelper().execute(builder);
//                getPhotoHelper().scanQrcode(new PhotoHelper.OnTakePhotoResult() {
//                    @Override
//                    public void onSuccess(Intent data, String path) {
//                        QdToast.show(mContext, path + "");
//                        //showScanDialog(path);
//                    }
//
//                    @Override
//                    public void onFailure(String error) {
//                        QdToast.show(mContext, "error:" + error);
//                    }
//                });
            }
        });
    }
}